<?php

class Contest_model extends MY_Model {
	private $table = 'contest';
    private $module = 'contest';
	
	function getsearchContent($limit,$page){
		$this->db->select('cli_contest.*,cli_users.username ,cli_users.email,cli_weeks.name as nameweek');
		$this->db->limit($limit,$page);
        $this->db->from('cli_contest');
        $this->db->join('cli_users', 'cli_users.id = cli_contest.user_id',"left");
        $this->db->join('cli_weeks', 'cli_weeks.id = cli_contest.week_id',"left");
		$this->db->order_by(PREFIX.$this->table.'.'.$this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post("week_id") != -1)
        {
            $this->db->where('cli_contest.week_id', $this->input->post("week_id"));
        }
        if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('`cli_users`.`username` LIKE "%'.$this->input->post('content'). '%" OR `cli_users`.`email` LIKE "%' . $this->input->post('content'). '%" OR `cli_contest`.`time` LIKE "%' . $this->input->post('content') . '%" OR `cli_contest`.`fb_id` LIKE "%'.$this->input->post('content') . '%" OR `cli_weeks`.`name` LIKE "%' . $this->input->post('content') . '%" OR `cli_contest`.`winner` LIKE "%' . $this->input->post('content') .'%" OR `cli_users`.`email` LIKE "%'.$this->input->post('content').'%"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where(PREFIX.$this->table.'.created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where(PREFIX.$this->table.'.created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where(PREFIX.$this->table.'.created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where(PREFIX.$this->table.'.created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
        
		//$this->db->group_by(PREFIX.$this->table.'.id');
     
		$query = $this->db->get();

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
        $this->db->from('cli_contest');
        $this->db->join('cli_users', 'cli_users.id = cli_contest.user_id',"left");  
        $this->db->join('cli_weeks', 'cli_weeks.id = cli_contest.week_id',"left");
        if($this->input->post("week_id") != -1)
        {
            $this->db->where('cli_contest.week_id', $this->input->post("week_id"));
        }
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
		$this->db->where('`cli_users`.`username` LIKE "%'.$this->input->post('content'). '%" OR `cli_users`.`email` LIKE "%' . $this->input->post('content'). '%" OR `cli_contest`.`time` LIKE "%' . $this->input->post('content') . '%" OR `cli_contest`.`fb_id` LIKE "%'.$this->input->post('content') . '%" OR `cli_weeks`.`name` LIKE "%' . $this->input->post('content') . '%" OR `cli_contest`.`winner` LIKE "%' . $this->input->post('content') .'%" OR `cli_users`.`email` LIKE "%'.$this->input->post('content').'%"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where(PREFIX.$this->table.'.created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where(PREFIX.$this->table.'.created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where(PREFIX.$this->table.'.created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where(PREFIX.$this->table.'.created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
       
        $query = $this->db->count_all_results();

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}
		if($this->input->post('hiddenIdAdmincp')==0){
			//Kiểm tra đã tồn tại chưa?
			$data = array(
				'content'=> $this->input->post('contentAdmincp'),
                'fb_id'=> $this->input->post('fb_idAdmincp'),
                'user_id'=> $this->input->post('usernameAdmincp'),
                'send_name'=> $this->input->post('send_nameAdmincp'),
                'receive_address'=> $this->input->post('send_addessAdmincp'),
				'thumb'=> $fileName['image'],
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			//Kiểm tra đã tồn tại chưa?
			
			//Xử lý xóa hình khi update thay đổi hình
			if($fileName['image']==''){
				$fileName['image'] = $result[0]->thumb;
			}else{
				
                    @unlink(BASEFOLDER.DIR_UPLOAD_CONTEST.$result[0]->thumb);
                        
                            
			}
			//End xử lý xóa hình khi update thay đổi hình
			
			$data = array(
				'content'=> $this->input->post('contentAdmincp'),
                'fb_id'=> $this->input->post('fb_idAdmincp'),
                'user_id'=> $this->input->post('usernameAdmincp'),
                'send_name'=> $this->input->post('send_nameAdmincp'),
                'receive_address'=> $this->input->post('send_addessAdmincp'),
				'thumb'=> $fileName['image'],
				'status'=> $status,
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
    function getContentContest($date_start, $date_end)
    {
        $this->db->select('cli_contest.*,cli_users.username ,cli_users.email');
        $this->db->from('cli_contest');
        $this->db->join('cli_users', 'cli_users.id = cli_contest.user_id',"left");  
        if ($date_start != '' && $date_end == '') {
            $this->db->where('cli_contest.created >= "' . date('Y-m-d 00:00:00', strtotime($date_start)) . '"');
        }
        if ($date_start == '' && $date_end != '') {
            $this->db->where('cli_contest.created <= "' . date('Y-m-d 23:59:59', strtotime($date_end)) . '"');
        }
        if ($date_start != '' && $date_end != '') {
            $this->db->where('cli_contest.created >= "' . date('Y-m-d 00:00:00', strtotime($date_start)) . '"');
            $this->db->where('cli_contest.created <= "' . date('Y-m-d 23:59:59', strtotime($date_end)) . '"');
        }
        $query = $this->db->get();

        if ($query->result()) {
            return $query->result();
        } else {
            return false;
        }
    }
    function list_users() {
        $this->db->select('*');
        $this->db->where('status', 1);
        $query = $this->db->get(PREFIX . 'users');
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return false;
    }
  
    
	function insert_user()
    {
        
         $fb_id = $_SESSION['fb_id'];
         $profile = $_SESSION['profile'] ;
         $gender ;
         if ($profile['gender'] == 'male')
         {
            $gender = 1;
         }
         else
         {
            $gender = 0;
         }
         $data = array(
            'fb_id' => $fb_id,
            'email' =>$profile['email'],
            'username' => $profile['username'],
            'fullname' => $profile['name'],
            'gender' => $gender,
            'ip'=>getIP(),
            'created' => date('Y-m-d H:i:s', time()),
            'status' => 1
        );
         if ($this->db->insert('cli_users', $data)) {
            return $this->db->insert_id();
        }
        return false;
        
    }
    function check_user($fb_id)
    {
        $this->db->select('id');
        $this->db->where('fb_id',$fb_id);
        $this->db->where('status', 1);
        $query = $this->db->get('cli_users');
        if ($query->num_rows() > 0) {
            return $query->num_rows();
        } else {
            return false;
        }
    }
    function get_user($fb_id)
    {
        $this->db->select('*');
        $this->db->where('fb_id',$fb_id);
        $this->db->where('status', 1);
        $query = $this->db->get('cli_users');
        if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    
    function get_weeks()
    {
        $this->db->select('*');
        $this->db->where('active',1);
        $this->db->where('status',1);
        $this->db->limit(1);
        $query = $this->db->get('cli_weeks');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
	function get_list_weeks()
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $query = $this->db->get('cli_weeks');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    function list_winner_weeks($week_id)
    {
        $this->db->select('cli_contest.*,cli_users.username ,cli_users.email');
        $this->db->from('cli_contest');
        $this->db->join('cli_users', 'cli_users.id = cli_contest.user_id',"left");
        $this->db->limit(10);
        $this->db->where('cli_contest.status',1);
        $this->db->where('cli_contest.winner',1);
        $this->db->where('cli_contest.week_id',$week_id);
        $this->db->order_by("time","asc");
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    function winner_weeks()
    {
        $this->db->select('cli_contest.week_id');
        $this->db->from('cli_contest');
        $this->db->where('cli_contest.status',1);
        $this->db->group_by('week_id');
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
   	function detail_weeks($id)
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $this->db->where('id',$id);
        $query = $this->db->get('cli_weeks');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    
    
    function list_top_score_day()
    {
       
        $query= $this->db->query(
			"SELECT a.time, a.created ,a.user_id ,a.fb_id, u.username,u.email from cli_contest a
			JOIN cli_users AS u ON a.user_id = u.id
			WHERE DATE(a.created) = CURDATE() AND time = (select MIN(time) from cli_contest where user_id = a.user_id AND DATE(created) = CURDATE()) AND a.status = 1
			GROUP BY a.user_id
			ORDER BY time asc ,a.created desc
			LIMIT 0,5"
		);

		if($query->result())
		{
			return $query->result();
		}
		else
		{
			return false;
		}		
    }
    function ran_top_score()
    {
       
        $query= $this->db->query(
			"SELECT a.time, a.created ,a.user_id ,a.fb_id, u.username,u.email from cli_contest a
			JOIN cli_users AS u ON a.user_id = u.id
			WHERE DATE(a.created) = CURDATE() AND time = (select MIN(time) from cli_contest where user_id = a.user_id AND DATE(created) = CURDATE()) AND a.status = 1
			GROUP BY a.user_id
			ORDER BY time asc ,a.created desc
            "
		);

		if($query->result())
		{
			return $query->result();
		}
		else
		{
			return false;
		}		
    }
    
    function get_luot_choi($user_id,$week_id)
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $this->db->where('week_id',$week_id);
        $this->db->where('fb_id',$user_id);
        $this->db->from('cli_contest_log');
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    function check_luot_choi($user_id)
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $this->db->where('fb_id',$user_id);
        $this->db->where('cli_contest_log.created >= "'.date('Y-m-d 00:00:00',time()).'"');
        $this->db->where('cli_contest_log.created <= "'.date('Y-m-d 23:59:59',time()).'"');
        $this->db->from('cli_contest_log');
	    $query = $this->db->count_all_results();
  	     if($query > 0){
			return $query;
		}else{
			return 0;
		}
    }
    function check_luot_choi_week($user_id,$week_id)
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $this->db->where('week_id',$week_id);
        $this->db->where('fb_id',$user_id);
        $this->db->from('cli_contest');
	    $query = $this->db->count_all_results();
  	     if($query > 0){
			return $query;
		}else{
			return 0;
		}
    }
    function update_status($fb_id,$week_id)
    {
         $update=array(
        				'status'=>0
        			);
         $this->db->where('week_id',$week_id);
         $this->db->where('fb_id',$fb_id);
         $this->db->update('cli_contest',$update);
         
    }
    function save_contest_log($user_id,$time,$fb_id,$week_id,$type,$contest_id)
    {
    
        $insert=array(
        				'user_id'=>$user_id,
        				'time'=>$time,
                        'fb_id'=>$fb_id,  
        				'week_id'=>$week_id,
        				'pizza_id'=>$type,
           	            'contest_id'=>$contest_id,
        				'status'=>1,
        				'created'=>date('Y-m-d H:i:s'),
                        'ip' =>getIP()
        			);
           $this->model->insert_id('contest_log',$insert);
    }
}